package com.lipp.luosu.avl;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName Main
 * @description: TODO 类描述
 * @Author lipp
 * @Date 2024/1/22 14:16
 * @Version 1.0
 */
@Slf4j
public class TestAvl {
    public static void main(String[] args) throws InterruptedException {
        AVLTree tree = new AVLTree();
        tree.root = new Node(10);
        tree.root = tree.insert(tree.root, 5);
        tree.root = tree.insert(tree.root, 15);
        tree.root = tree.insert(tree.root, 12);
        tree.root = tree.insert(tree.root, 17);
        tree.root = tree.insert(tree.root, 13);
        tree.root = tree.insert(tree.root, 11);
        tree.root = tree.insert(tree.root, 14);

        preOrder(tree.root);
        System.out.println("前序遍历");
        inOrder(tree.root);
        System.out.println("中序遍历");
        postOrder(tree.root);
        System.out.println("后序遍历");

        log.info("删除节点17");
        Node delete = tree.delete(tree.root, 17);

        preOrder(tree.root);
        System.out.println("前序遍历");
        inOrder(tree.root);
        System.out.println("中序遍历");
        postOrder(tree.root);
        System.out.println("后序遍历");
    }

    public static void preOrder(Node root) {
        if (root == null) return;

        System.out.print(root.key + " "); // 先打印根节点
        preOrder(root.left); // 再递归遍历左子树
        preOrder(root.right); // 最后递归遍历右子树
    }

    public static void inOrder(Node root) {
        if (root == null) return;

        inOrder(root.left); // 先递归遍历左子树
        System.out.print(root.key + " "); // 然后打印根节点
        inOrder(root.right); // 最后递归遍历右子树
    }

    public static void postOrder(Node root) {
        if (root == null) return;

        postOrder(root.left); // 先递归遍历左子树
        postOrder(root.right); // 然后递归遍历右子树
        System.out.print(root.key + " "); // 最后打印根节点
    }

}
